package com.thinvent.nj.ais.service.impl;

import com.thinvent.nj.common.exception.ServiceException;
import com.thinvent.nj.common.page.Page;
import com.thinvent.nj.common.page.QueryParameter;
import com.thinvent.nj.mybatis.service.impl.BaseCURDServiceImpl;
import com.thinvent.nj.ais.entity.Org;
import com.thinvent.nj.ais.mapper.OrgMapper;
import com.thinvent.nj.ais.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 组织服务
 * @author liupj
 */
@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class OrgServiceImpl extends BaseCURDServiceImpl<Org, String> implements OrgService {

    @Autowired
    private OrgMapper orgMapper;


    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Org> getAllOrgListByPId(String pId, Map<String, Object> params) {
        if (pId == null) {
            throw new ServiceException("pId cannot be null. root pId is -1 . ");
        }

        List<Org> subList = orgMapper.getAllOrgListByPId(pId);
        return subList;
    }


    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Org> getOrgListByCondition(Map<String, Object> params) {
        return orgMapper.getOrgListByCondition(params);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Org> getOrgListByUserId(String userId) {
        return orgMapper.getOrgListByUserId(userId);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Org get(String s) {
        return super.get(s);
    }

    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String s) {
        super.delete(s);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void update(Org org) {
        super.update(org);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "sys", allEntries = true)
    @Override
    public void insert(Org org) {
        super.insert(org);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public List<Org> findList(Map<String, Object> params) {
        return super.findList(params);
    }

    @Cacheable(cacheNames = "sys", keyGenerator = "keyGenerator")
    @Override
    public Page<Org> findPage(QueryParameter queryParameter, Map<String, Object> params) {
        return super.findPage(queryParameter, params);
    }
}
